﻿			 ----------------
			------LOCAWEB------
			 ----------------

CREATE USER sistemachamados PASSWORD 'JyzgI3vA3D' ;

CREATE DATABASE sistemachamados
  
	WITH OWNER = sistemachamados
       
	ENCODING = 'UTF8'
       
	TABLESPACE = pg_default
       
	LC_COLLATE = 'Portuguese_Brazil.1252'
       
	LC_CTYPE = 'Portuguese_Brazil.1252'
       
	CONNECTION LIMIT = -1;


--INICIO DA CRIAÇÃO DO BD
----------------------------------
-----------------------------------------
-----------------------------------------------

------------
-- TABLES --
------------

--STATE--
--SELECT * FROM TBL_STATE;
--DROP TABLE TBL_STATE CASCADE;
CREATE TABLE TBL_STATE(
  TBL_STATEID serial not null,
  TBL_STATENAME varchar(100) not null,
  TBL_STATEACRONYM varchar(10) not null,
  CONSTRAINT TBL_STATE_PK PRIMARY KEY (TBL_STATEID)    
);

--CITY--
--SELECT * FROM TBL_CITY;
--DROP TABLE TBL_CITY CASCADE;
CREATE TABLE TBL_CITY
(
  TBL_CITYID serial not null,
  TBL_CITYDESCRIPTION character (50) not null,
  TBL_STATEID integer not null,
  CONSTRAINT TBL_CITY_PK PRIMARY KEY (TBL_CITYID) 
);
ALTER TABLE TBL_CITY ADD CONSTRAINT TBL_CITY_FK FOREIGN KEY (TBL_STATEID) REFERENCES TBL_STATE(TBL_STATEID);


--CITYSTATE AUDITING--
--SELECT * FROM TBL_CITYSTATEAUDIT;
--DROP TABLE TBL_CITYSTATEAUDIT CASCADE;
CREATE TABLE TBL_CITYSTATEAUDIT ( 
  TBL_CITYSTATEAUDITID serial not null,
  TBL_CITYID integer not null,
  TBL_CITYDESCRIPTION character (50) default null,
  TBL_STATEID integer not null,
  TBL_STATENAME varchar(100) default null,
  TBL_STATEACRONYM varchar(10) default null,
  TBL_CITYSTATEAUDITUSER character (20) not null,
  TBL_CITYSTATEAUDITACTION character (10) not null,
  TBL_CITYSTATEAUDITDATE timestamp default NOW(),
  CONSTRAINT TBL_CITYSTATEAUDITID_PK PRIMARY KEY (TBL_CITYSTATEAUDITID)
);

--PLACE--
--SELECT * FROM TBL_PLACE;
--DROP TABLE TBL_PLACE CASCADE;
CREATE TABLE TBL_PLACE(
  TBL_PLACEID serial not null,
  TBL_PLACEZIP integer not null,
  TBL_PLACEADDRESS varchar (100) not null,
  TBL_PLACECOMPLEMENT varchar (100) default null,
  TBL_PLACENUMBER integer not null,
  TBL_CITYID integer not null,
  CONSTRAINT TBL_PLACE_PK PRIMARY KEY (TBL_PLACEID)
);
ALTER TABLE TBL_PLACE ADD CONSTRAINT TBL_PLACE_FK FOREIGN KEY (TBL_CITYID) REFERENCES TBL_CITY(TBL_CITYID);

--PLACECITY AUDITING--
--SELECT * FROM TBL_PLACECITYAUDIT;
--DROP TABLE TBL_PLACECITYAUDIT CASCADE;
CREATE TABLE TBL_PLACECITYAUDIT ( 
  TBL_PLACECITYAUDITID serial not null,
  TBL_PLACEID integer not null,
  TBL_PLACEZIP integer default null,
  TBL_PLACEADDRESS varchar (100) default null,
  TBL_PLACECOMPLEMENT varchar (100) default null,
  TBL_PLACENUMBER integer default null,
  TBL_CITYID integer not null,
  TBL_CITYDESCRIPTION character (50) default null,
  TBL_PLACECITYAUDITUSER character (20) default null,
  TBL_PLACECITYAUDITACTION character (10) default null,
  TBL_PLACECITYAUDITDATE timestamp default NOW(),
  CONSTRAINT TBL_PLACECITYAUDIT_PK PRIMARY KEY (TBL_PLACECITYAUDITID)
);

--CALLCATEGORY--
--SELECT * FROM TBL_CALLCATEGORY;
--DROP TABLE TBL_CALLCATEGORY CASCADE;
CREATE TABLE TBL_CALLCATEGORY
(
  TBL_CALLCATEGORYID serial not null,
  TBL_CALLCATEGORYDESCRIPTION character (200) not null,
  TBL_CALLCATEGORYMINTIMEANALIZE double precision not null,
  TBL_CALLCATEGORYMAXTIMEANALIZE double precision not null,
  TBL_CALLCATEGORYMINTIMETREATMENT double precision not null,
  TBL_CALLCATEGORYMAXTIMETREATMENT double precision not null,
  CONSTRAINT TBL_CALLCATEGORY_PK PRIMARY KEY (TBL_CALLCATEGORYID )
);

--UNIT--
--SELECT * FROM TBL_UNIT;
--DROP TABLE TBL_UNIT CASCADE;
CREATE TABLE TBL_UNIT(
  TBL_UNITID serial not null,
  TBL_UNITNAME character (100) not null,
  TBL_PLACEID integer not null,
  CONSTRAINT TBL_UNIT_PK PRIMARY KEY (TBL_UNITID)
);

ALTER TABLE TBL_UNIT ADD CONSTRAINT TBL_UNIT_FK FOREIGN KEY (TBL_PLACEID) REFERENCES TBL_PLACE(TBL_PLACEID);

--SECTOR--
--SELECT * FROM TBL_SECTOR;
--DROP TABLE TBL_SECTOR CASCADE;
CREATE TABLE TBL_SECTOR(
  TBL_SECTORID serial not null,
  TBL_SECTORDESCRIPTION character (100) not null,
  TBL_UNITID integer not null,
  CONSTRAINT TBL_SECTOR_PK PRIMARY KEY (TBL_SECTORID)
);

ALTER TABLE TBL_SECTOR ADD CONSTRAINT TBL_SECTOR_FK FOREIGN KEY (TBL_UNITID) REFERENCES TBL_UNIT(TBL_UNITID);

--UNITPLACE AUDITING--
--SELECT * FROM TBL_UNITPLACEAUDIT;
--DROP TABLE TBL_UNITPLACEAUDIT CASCADE;
CREATE TABLE TBL_UNITPLACEAUDIT ( 
  TBL_UNITPLACEAUDITID serial not null,
  TBL_UNITID integer not null,
  TBL_UNITNAME character (100) default null,
  TBL_PLACEID integer not null,
  TBL_PLACEZIP integer default null,
  TBL_PLACEADDRESS varchar (100) default null,
  TBL_PLACECOMPLEMENT varchar (100) default null,
  TBL_PLACENUMBER integer default null,
  TBL_UNITPLACEAUDITUSER character (20) default null,
  TBL_UNITPLACEAUDITACTION character (10) default null,
  TBL_UNITPLACEAUDITDATE timestamp default NOW(),
  CONSTRAINT TBL_UNITPLACEAUDIT_PK PRIMARY KEY (TBL_UNITPLACEAUDITID)
);

--USER--
--SELECT * FROM TBL_USER;
--DROP TABLE TBL_USER CASCADE;
CREATE TABLE TBL_USER(
  TBL_USERID serial not null,
  TBL_USERNAME character (150) not null,
  TBL_USERLOGIN character (50) not null,
  TBL_USERPASSWORD character (50) not null,
  TBL_SECTORID integer not null,
  CONSTRAINT TBL_USER_PK PRIMARY KEY (TBL_USERID)
);
ALTER TABLE TBL_USER ADD CONSTRAINT TBL_USER_FK FOREIGN KEY (TBL_SECTORID) REFERENCES TBL_SECTOR(TBL_SECTORID);

--CALLSTATUS-
--SELECT * FROM TBL_CALLSTATUS;
--DROP TABLE TBL_CALLSTATUS CASCADE;
CREATE TABLE TBL_CALLSTATUS(
  TBL_CALLSTATUSID serial not null,
  TBL_CALLSTATUSDESCRIPTION character (20) not null,
  CONSTRAINT TBL_CALLSTATUS_PK PRIMARY KEY (TBL_CALLSTATUSID)
);

--CALL--
--SELECT * FROM TBL_CALL;
--DROP TABLE TBL_CALL CASCADE;
CREATE TABLE TBL_CALL ( 
  TBL_CALLID integer not null,
  TBL_CALLDATEOPENING timestamp not null default NOW(),
  TBL_CALLDATECLOSING timestamp default null,
  TBL_CALLPROBLEMDESCRIPTION varchar (500) not null,
  TBL_CALLSOLUTIONDESCRIPTION varchar (500) default null,
  TBL_USEROPEN integer not null,
  TBL_USERCLOSE integer default null,
  TBL_CALLSTATUSID integer not null,
  TBL_CALLCATEGORYID integer not null,
  TBL_SECTORID integer not null,
  CONSTRAINT TBL_CALL_PK PRIMARY KEY (TBL_CALLID)
);

  
ALTER TABLE TBL_CALL ADD CONSTRAINT TBL_CALLUSEROPEN_FK FOREIGN KEY (TBL_USEROPEN) REFERENCES TBL_USER(TBL_USERID);
ALTER TABLE TBL_CALL ADD CONSTRAINT TBL_CALLUSERCLOSE_FK FOREIGN KEY (TBL_USERCLOSE) REFERENCES TBL_USER(TBL_USERID);
ALTER TABLE TBL_CALL ADD CONSTRAINT TBL_CALLSTATUS_FK FOREIGN KEY (TBL_CALLSTATUSID) REFERENCES TBL_CALLSTATUS(TBL_CALLSTATUSID);
ALTER TABLE TBL_CALL ADD CONSTRAINT TBL_CALLCATEGORY_FK FOREIGN KEY (TBL_CALLCATEGORYID) REFERENCES TBL_CALLCATEGORY(TBL_CALLCATEGORYID);
ALTER TABLE TBL_CALL ADD CONSTRAINT TBL_CALLSECTOR_FK FOREIGN KEY (TBL_SECTORID) REFERENCES TBL_SECTOR(TBL_SECTORID);

--ANNEX--
--SELECT * FROM TBL_ANNEX;
--DROP TABLE TBL_ANNEX CASCADE;
CREATE TABLE TBL_ANNEX ( 
  TBL_ANNEXID serial not null,
  TBL_ANNEXDESCRIPTION varchar (9000) default null,
  TBL_CALLID integer not null,
  CONSTRAINT TBL_ANNEXID_PK PRIMARY KEY (TBL_ANNEXID)
);

ALTER TABLE TBL_ANNEX ADD CONSTRAINT TBL_ANNEX_FK FOREIGN KEY (TBL_CALLID) REFERENCES TBL_CALL(TBL_CALLID);

---------------------
-- FUNTION & VIEWS --
---------------------

--CITYSTATE
--DROP FUNCTION FUN_CITYSTATEAUDIT() CASCADE;
--CREATE LANGUAGE PLPGSQL;
CREATE OR REPLACE FUNCTION FUN_CITYSTATEAUDIT() RETURNS trigger AS 
$$
  DECLARE
    RESTBL_CITYID integer;
    RESTBL_CITYDESCRIPTION varchar(100);
    RESTBL_STATEID integer;
    RESTBL_STATENAME varchar(100);
    RESTBL_STATEACRONYM varchar(10);
    RES RECORD;
  BEGIN
    IF TG_OP = 'UPDATE' THEN       
       FOR RES IN SELECT * FROM TBL_STATE as TB1 INNER JOIN TBL_CITY as TB2 on TB1.TBL_STATEID=TB2.TBL_STATEID WHERE TB1.TBL_STATEID=NEW.TBL_STATEID LOOP       
          RESTBL_CITYID := RES.TBL_CITYID;
          RESTBL_CITYDESCRIPTION := RES.TBL_CITYDESCRIPTION;
          RESTBL_STATEID := RES.TBL_STATEID;
          RESTBL_STATENAME := RES.TBL_STATENAME;
          RESTBL_STATEACRONYM := RES.TBL_STATEACRONYM;       
         INSERT INTO TBL_CITYSTATEAUDIT (TBL_CITYID, TBL_CITYDESCRIPTION, TBL_STATEID, TBL_STATENAME, TBL_STATEACRONYM, TBL_CITYSTATEAUDITUSER, TBL_CITYSTATEAUDITACTION ) VALUES (RESTBL_CITYID, RESTBL_CITYDESCRIPTION, RESTBL_STATEID, RESTBL_STATENAME, RESTBL_STATEACRONYM, USER, TG_OP);
       END LOOP;
    ELSE
	IF TG_OP = 'DELETE' THEN
	   INSERT INTO TBL_CITYSTATEAUDIT (TBL_CITYID, TBL_CITYDESCRIPTION, TBL_STATEID, TBL_STATENAME, TBL_STATEACRONYM, TBL_CITYSTATEAUDITUSER, TBL_CITYSTATEAUDITACTION, TBL_CITYSTATEAUDITDATE ) VALUES (OLD.TBL_CITYID, NULL , OLD.TBL_STATEID , NULL, NULL, USER, TG_OP, NOW());
        END IF;
    END IF;    
    RETURN NEW;
  END;
$$ 
LANGUAGE plpgsql;

CREATE TRIGGER TRI_CITYSTATEAUDIT AFTER DELETE OR UPDATE ON TBL_CITY FOR EACH ROW  EXECUTE PROCEDURE FUN_CITYSTATEAUDIT();
CREATE TRIGGER TRI_STATECITYAUDIT AFTER DELETE OR UPDATE ON TBL_STATE FOR EACH ROW  EXECUTE PROCEDURE FUN_CITYSTATEAUDIT();

CREATE OR REPLACE VIEW VIE_CITYSTATEAUDIT AS SELECT TBL_CITYID AS ID_CIDADE, TBL_CITYDESCRIPTION AS CIDADE, TBL_STATEID AS ID_ESTADO,  TBL_STATENAME AS ESTADO, TBL_STATEACRONYM AS UF, TBL_CITYSTATEAUDITUSER AS RESPONSAVEL, TBL_CITYSTATEAUDITACTION AS ACAO, TBL_CITYSTATEAUDITDATE AS DATA  FROM TBL_CITYSTATEAUDIT ORDER BY TBL_CITYSTATEAUDITID DESC;


--PLACECITY
--DROP FUNCTION FUN_PLACECITYAUDIT() CASCADE;
--CREATE LANGUAGE PLPGSQL;
CREATE OR REPLACE FUNCTION FUN_PLACECITYAUDIT() RETURNS trigger AS 
$$
  DECLARE
    RESTBL_PLACEID integer;
    RESTBL_PLACEZIP integer;
    RESTBL_PLACEADDRESS varchar (100);
    RESTBL_PLACECOMPLEMENT varchar (100);
    RESTBL_PLACENUMBER integer;
    RESTBL_CITYID integer;
    RESTBL_CITYDESCRIPTION character (50);
    RES RECORD;
  BEGIN
    IF TG_OP = 'UPDATE' THEN       
       FOR RES IN SELECT * FROM TBL_CITY as TB1 INNER JOIN TBL_PLACE as TB2 on TB1.TBL_CITYID=TB2.TBL_CITYID WHERE TB1.TBL_CITYID=NEW.TBL_CITYID LOOP       
	  RESTBL_PLACEID := RES.TBL_PLACEID;
	  RESTBL_PLACEZIP := RES.TBL_PLACEZIP;
	  RESTBL_PLACEADDRESS := RES.TBL_PLACEADDRESS;
	  RESTBL_PLACECOMPLEMENT := RES.TBL_PLACECOMPLEMENT;
	  RESTBL_PLACENUMBER := RES.TBL_PLACENUMBER;
	  RESTBL_CITYID := RES.TBL_CITYID;
          RESTBL_CITYDESCRIPTION := RES.TBL_CITYDESCRIPTION;
          INSERT INTO TBL_PLACECITYAUDIT (TBL_PLACEID, TBL_PLACEZIP, TBL_PLACEADDRESS, TBL_PLACECOMPLEMENT, TBL_PLACENUMBER, TBL_CITYID, TBL_CITYDESCRIPTION, TBL_PLACECITYAUDITUSER, TBL_PLACECITYAUDITACTION ) VALUES (RESTBL_PLACEID, RESTBL_PLACEZIP, RESTBL_PLACEADDRESS, RESTBL_PLACECOMPLEMENT, RESTBL_PLACENUMBER, RESTBL_CITYID, RESTBL_CITYDESCRIPTION, USER, TG_OP);
       END LOOP;
    ELSE
	IF TG_OP = 'DELETE' THEN
	   INSERT INTO TBL_PLACECITYAUDIT (TBL_PLACEID, TBL_PLACEZIP, TBL_PLACEADDRESS, TBL_PLACECOMPLEMENT, TBL_PLACENUMBER, TBL_CITYID, TBL_CITYDESCRIPTION, TBL_PLACECITYAUDITUSER, TBL_PLACECITYAUDITACTION, TBL_PLACECITYAUDITDATE) VALUES (OLD.TBL_PLACEID, NULL , NULL, NULL, NULL, OLD.TBL_CITYID , NULL, USER, TG_OP, NOW());
        END IF;
    END IF;    
    RETURN NEW;
  END;
$$ 
LANGUAGE plpgsql;


CREATE TRIGGER TRI_PLACECITYAUDIT AFTER DELETE OR UPDATE ON TBL_PLACE FOR EACH ROW  EXECUTE PROCEDURE FUN_PLACECITYAUDIT();
CREATE TRIGGER TRI_CITYPLACEAUDIT AFTER DELETE OR UPDATE ON TBL_CITY FOR EACH ROW  EXECUTE PROCEDURE FUN_PLACECITYAUDIT();

CREATE OR REPLACE VIEW VIE_PLACECITYAUDIT AS SELECT TBL_PLACEID AS ID_LOCAL, TBL_PLACEZIP AS CEP, TBL_PLACEADDRESS AS ENDERECO,  TBL_PLACECOMPLEMENT AS COMPLEMENTO, TBL_PLACENUMBER AS NRO_LOCAL, TBL_CITYID AS ID_CIDADE, TBL_CITYDESCRIPTION AS CIDADE, TBL_PLACECITYAUDITUSER AS RESPONSAVEL, TBL_PLACECITYAUDITACTION AS ACAO, TBL_PLACECITYAUDITDATE AS DATA  FROM TBL_PLACECITYAUDIT ORDER BY TBL_PLACECITYAUDITID DESC;


--UNITPLACE
--DROP FUNCTION FUN_UNITPLACEAUDIT() CASCADE;
CREATE OR REPLACE FUNCTION FUN_UNITPLACEAUDIT()
  RETURNS trigger AS
$$
  DECLARE
    RESTBL_UNITID integer;
    RESTBL_UNITNAME varchar (100);
    RESTBL_PLACEID integer;
    RESTBL_PLACEZIP integer;
    RESTBL_PLACEADDRESS character (100);
    RESTBL_PLACECOMPLEMENT character (100);  
    RESTBL_PLACENUMBER integer;
    RES RECORD;
  BEGIN
    IF TG_OP = 'UPDATE' THEN       
       FOR RES IN SELECT * FROM TBL_UNIT as TB1 INNER JOIN TBL_PLACE as TB2 on TB1.TBL_PLACEID=TB2.TBL_PLACEID WHERE TB1.TBL_PLACEID=NEW.TBL_PLACEID LOOP   
	  RESTBL_UNITID := RES.TBL_UNITID;
	  RESTBL_UNITNAME := RES.TBL_UNITNAME;
	  RESTBL_PLACEID := RES.TBL_PLACEID;
	  RESTBL_PLACEZIP := RES.TBL_PLACEZIP;
	  RESTBL_PLACEADDRESS := RES.TBL_PLACEADDRESS;
	  RESTBL_PLACECOMPLEMENT := RES.TBL_PLACECOMPLEMENT;
	  RESTBL_PLACENUMBER := RES.TBL_PLACENUMBER;
          INSERT INTO TBL_UNITPLACEAUDIT (TBL_UNITID, TBL_UNITNAME, TBL_PLACEID, TBL_PLACEZIP, TBL_PLACEADDRESS, TBL_PLACECOMPLEMENT, TBL_PLACENUMBER, TBL_UNITPLACEAUDITUSER, TBL_UNITPLACEAUDITACTION) VALUES (RESTBL_UNITID, RESTBL_UNITNAME, RESTBL_PLACEID, RESTBL_PLACEZIP, RESTBL_PLACEADDRESS, RESTBL_PLACECOMPLEMENT, RESTBL_PLACENUMBER, USER, TG_OP);
       END LOOP;
    ELSE
	IF TG_OP = 'DELETE' THEN
	   INSERT INTO TBL_UNITPLACEAUDIT (TBL_UNITID, TBL_UNITNAME, TBL_PLACEID, TBL_PLACEZIP, TBL_PLACEADDRESS, TBL_PLACECOMPLEMENT, TBL_PLACENUMBER, TBL_UNITPLACEAUDITUSER, TBL_UNITPLACEAUDITACTION, TBL_UNITPLACEAUDITDATE) VALUES (OLD.TBL_UNITID, NULL ,OLD.TBL_PLACEID , NULL, NULL, NULL , NULL, USER, TG_OP, NOW());
        END IF;
    END IF;    
    RETURN NEW;
  END;
$$
LANGUAGE plpgsql; 


CREATE TRIGGER TRI_UNITPLACEAUDIT AFTER DELETE OR UPDATE ON TBL_UNIT FOR EACH ROW  EXECUTE PROCEDURE FUN_UNITPLACEAUDIT();
CREATE TRIGGER TRI_PLACEUNITAUDIT AFTER DELETE OR UPDATE ON TBL_PLACE FOR EACH ROW  EXECUTE PROCEDURE FUN_UNITPLACEAUDIT();

CREATE OR REPLACE VIEW VIE_UNITPLACEAUDIT AS 
 SELECT TBL_UNITID AS ID_UNIDADE, TBL_UNITNAME AS UNIDADE, TBL_PLACEID AS ID_LOCAL, TBL_PLACEZIP AS CEP, TBL_PLACEADDRESS AS ENDERECO, TBL_PLACECOMPLEMENT AS COMPLEMENTO, TBL_PLACENUMBER AS NUMERO, TBL_UNITPLACEAUDITUSER AS RESPONSAVEL, TBL_UNITPLACEAUDITACTION AS ACAO, TBL_UNITPLACEAUDITDATE AS DATA FROM TBL_UNITPLACEAUDIT ORDER BY TBL_UNITPLACEAUDITID DESC;


--CALLSEQUENCE
CREATE SEQUENCE SEQ_CALLID START 1 INCREMENT BY 1 ; 
--DROP SEQUENCE SEQ_CALLID;
--DROP FUNCTION FUN_GETCALLID() CASCADE;
--SELECT * FROM FUN_GETCALLID();
--CREATE LANGUAGE PLPGSQL;
CREATE OR REPLACE FUNCTION FUN_GETCALLID() RETURNS INTEGER AS 
$$
  DECLARE
    RESSEQ_CALLID integer;
    RES RECORD;
  BEGIN
    FOR RES IN SELECT NEXTVAL('SEQ_CALLID') LOOP   
	  RESSEQ_CALLID := RES.NEXTVAL;
    END LOOP;
    RETURN RESSEQ_CALLID;
  END;
$$ 
LANGUAGE plpgsql;


--FIM DA CRIAÇÃO DO BD
----------------------------------
-----------------------------------------
-----------------------------------------------

-DROP GERAL   -- DROP DE TUDO DO BD!
DROP TABLE TBL_STATE CASCADE;
DROP TABLE TBL_CITY CASCADE;
DROP TABLE TBL_CITYSTATEAUDIT CASCADE;
DROP TABLE TBL_PLACE CASCADE;
DROP TABLE TBL_PLACECITYAUDIT CASCADE;
DROP TABLE TBL_CALLCATEGORY CASCADE;
DROP TABLE TBL_UNIT CASCADE;
DROP TABLE TBL_SECTOR CASCADE;
DROP TABLE TBL_UNITPLACEAUDIT CASCADE;
DROP TABLE TBL_USER CASCADE;
DROP TABLE TBL_CALLSTATUS CASCADE;
DROP TABLE TBL_CALL CASCADE;
DROP TABLE TBL_ANNEX CASCADE;
DROP FUNCTION FUN_CITYSTATEAUDIT() CASCADE;
DROP FUNCTION FUN_PLACECITYAUDIT() CASCADE;
DROP FUNCTION FUN_UNITPLACEAUDIT() CASCADE;
DROP SEQUENCE SEQ_CALLID;
DROP FUNCTION FUN_GETCALLID() CASCADE;

--SELECT GERAL
SELECT * FROM TBL_STATE;
SELECT * FROM  TBL_CITY;
SELECT * FROM  TBL_CITYSTATEAUDIT;
SELECT * FROM  TBL_PLACE;
SELECT * FROM  TBL_PLACECITYAUDIT;
SELECT * FROM  VIE_PLACECITYAUDIT;
SELECT * FROM  TBL_CALLCATEGORY;
SELECT * FROM  TBL_UNIT;
SELECT * FROM  TBL_SECTOR;
SELECT * FROM  TBL_UNITPLACEAUDIT;
SELECT * FROM  TBL_USER;
SELECT * FROM  TBL_CALLSTATUS;
SELECT * FROM  TBL_CALL;
SELECT * FROM  TBL_ANNEX;
SELECT * FROM  FUN_CITYSTATEAUDIT();
SELECT * FROM  FUN_PLACECITYAUDIT();
SELECT * FROM  FUN_UNITPLACEAUDIT();
SELECT * FROM  SEQ_CALLID;
SELECT * FROM  FUN_GETCALLID();

